package leetcode_top;
import java.util.*;
import org.junit.*;
public class Ex166 {
    class Solution {
        //对于-无穷大没有考虑到，因为需要将其转为正数的话就溢出了，需要使用long接收
        public String fractionToDecimal(int numerator, int denominator) {
            boolean flag = true;
            long a = 0, b = 0;
            if (numerator > 0 && denominator < 0) {
                flag = false;
            } else if (numerator < 0 && denominator > 0) {
                flag = false;
            } else if (numerator == 0) return "0";
            a = Math.abs((long)denominator);
            b = Math.abs((long)numerator);
            
            String left = "";
            long l = b / a;
            left = l + ".";
            long t = b % a, n;
            Map<Long, Integer> map = new HashMap<>();
            StringBuilder sb = new StringBuilder();
            int i = 0;
            while (t != 0) {

                if (map.containsKey(t)) {
                    int idx = map.get(t);
                    sb.insert(idx, '(');
                    sb.append(')');
                    break;
                }
                
                map.put(t, i);
                t *= 10;
                n = t / a;
                sb.append(n);
                i++;
                t %= a;
            }
            if (!flag) {
                left = "-" + left;
            }
            sb.insert(0, left);
            String s = sb.toString();
            if (s.endsWith(".")) {
                s = s.substring(0, s.length() - 1);
            }
            return s;
        }

        public String fractionToDecimal3(int numerator, int denominator) {
            if (numerator == 0) {
                return "0";
            }
            StringBuilder fraction = new StringBuilder();
            // If either one is negative (not both)
            if (numerator < 0 ^ denominator < 0) {
                fraction.append("-");
            }
            // Convert to Long or else abs(-2147483648) overflows
            long dividend = Math.abs(Long.valueOf(numerator));
            long divisor = Math.abs(Long.valueOf(denominator));
            fraction.append(String.valueOf(dividend / divisor));
            long remainder = dividend % divisor;
            if (remainder == 0) {
                return fraction.toString();
            }
            fraction.append(".");
            Map<Long, Integer> map = new HashMap<>();
            while (remainder != 0) {
                if (map.containsKey(remainder)) {
                    fraction.insert(map.get(remainder), "(");
                    fraction.append(")");
                    break;
                }
                map.put(remainder, fraction.length());
                remainder *= 10;
                fraction.append(String.valueOf(remainder / divisor));
                remainder %= divisor;
            }
            return fraction.toString();
        }
        
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.fractionToDecimal(-1, -2147483648));     
        System.out.println(s.fractionToDecimal3(-1, -2147483648));        

    }
}
